import { Customer } from './../../domains/customer.domain';
import { OnInit } from '@angular/core';
import { CustomerServiceSvr } from '../../service/register.service'
import { Component } from '@angular/core';
import { Router } from '@angular/router';
import { HouseList } from './../../domains/houselist.domain';

@Component({
  selector: 'app-rent-management',
  templateUrl: './rent-management.component.html',
  styleUrls: ['./rent-management.component.less']
})


export class RentManagementComponent implements OnInit{
  constructor(private router:Router, private customer: CustomerServiceSvr){}

  token: string = JSON.parse(localStorage.getItem('Token')!)
  customerId: string = JSON.parse(localStorage.getItem('customerId')!)
  result: any
  houseList = new HouseList()
  loading = true;

  openContract(item: any) {
    console.log(item)
    this.router.navigate(['/personal-center/rental-information'], {
      queryParams: {
        houseData: JSON.stringify(item)
      }
    })
  }

  async ngOnInit() {
    this.getCustomerRentHouseList()
  }

  // 获取用户的租房列表
  async getCustomerRentHouseList() {
   try {
    this.result = await this.customer.listHousesByCustomerId(this.token, this.customerId, 1, -1)
   } catch (error) {
    console.log(error)
   }
    this.houseList = this.result._Items
    this.houseList.map((element: { CreateTime: string | number | Date; }) => {
      element.CreateTime = this.formatTime(element.CreateTime)
    });
    console.log(this.houseList)
  }

  // 格式化时间
  formatTime(time: string | number | Date) {
    time = new Date(time)
    let year = time.getFullYear()
    let month = time.getMonth() + 1
    let date = time.getDate()
    return year+'-'+month+'-'+date
  }
}
